<template>
  <div class="bottom-bar">
    <div class="check-content">
      <check-button
        class="check-button"
        :is-checked="isSelectAll"
        @click.native="checkClinck"
      ></check-button>
      <span>全选</span>
    </div>
    <div class="total-price">合计:￥{{ totalPrice }}</div>
    <div class="caculate" @click="caculateClick">去结算({{ checkLength }})</div>
    <toast :message="message" :isShow="show" />
  </div>
</template>

<script>
import CheckButton from "./CheckButton";
import Toast from "components/common/toast/Toast";
export default {
  name: "CarBottomBar",
  components: {
    CheckButton,
    Toast,
  },
  data() {
    return {
      message: "",
      show: false,
    };
  },
  computed: {
    totalPrice() {
      return this.$store.state.cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((preValue, item) => {
          return preValue + item.count * item.newPrice;
        }, 0)
        .toFixed(2);
    },
    checkLength() {
      return this.$store.state.cartList.filter((item) => item.checked).length;
    },
    isSelectAll() {
      //全部遍历
      //   return !this.$store.state.cartList.filter((item) => !item.checked).length;
      //找到就停止（数组的告诫函数）
      if (this.$store.state.cartList.length === 0) return false;
      return !this.$store.state.cartList.find((item) => !item.checked);
      //普通遍历for
    },
  },
  methods: {
    checkClinck() {
      if (this.isSelectAll) {
        this.$store.state.cartList.forEach((item) => {
          item.checked = false;
        });
      } else {
        this.$store.state.cartList.forEach((item) => {
          item.checked = true;
        });
      }
      //不能简化
      //   this.$store.state.cartList.forEach((item) => {
      //     item.checked = !this.isSelectAll;
      //   });
    },
    caculateClick() {
      if (this.checkLength == 0) {
        this.show = true;
        this.message = "请添加商品";
      } else {
        this.show = true;
        this.message = "该功能暂未开放";
      }
      setTimeout(() => {
        this.show = false;
        this.message = "";
      }, 1500);
    },
  },
};
</script>

<style scoped>
.bottom-bar {
  position: relative;
  display: flex;

  height: 40px;
  line-height: 40px;
  background-color: #eee;
}
.check-content {
  display: flex;
  align-items: center;
  width: 90px;
  margin-left: 5px;
}
.check-button {
  width: 22px;
  height: 22px;
  line-height: 22px;
  margin: 0 10px;
}
.total-price {
  flex: 1;
}
.caculate {
  width: 90px;
  background-color: red;
  color: #fff;
  text-align: center;
}
</style>